package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import dao.SaleDao;
import domain.Order;
import domain.Product;
import domain.User;
import domain.sales;
import util.DataSourceUtils;

public class SaleDaoImpl implements SaleDao {

	@Override
	public List<sales> findAll() throws SQLException {
		String sql = "select orderitem.buynum, products.name from orderitem inner join products on orderitem.product_id=products.id order by orderitem.buynum desc;";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		return runner.query(sql, new ResultSetHandler<List<sales>> () {

			@Override
			public List<sales> handle(ResultSet rs) throws SQLException {
				List<sales> res = new ArrayList<sales>();
				
				while (rs.next()) {
					sales s = new sales();
					s.setName(rs.getString(2));
					s.setBuynum(rs.getInt(1));
					res.add(s);
				}
				
				return res;
			}
			
		});
	}

}
